// pages/category/category.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    categoryList: [],
    domain: getApp().globalData.domain,
    highlight: [],
    subList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCategory()
    this.setHighlight()
    this.getSubList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // console.log(getApp().globalData.globalId)
    var pid = getApp().globalData.globalId
    var index = getApp().globalData.globalId-1
    // console.log(index)
    this.setHighlight(index)
    var that = this;
    wx.request({
      url: getApp().globalData.domain + 'api/commodity/list?category_id=' + pid,
      data: { pid: pid },
      header: {
        "Content-Type": "applciation/json"
      },
      method: "GET",
      success: function (res) {
        // console.log(res.data.data);
        that.setData({
          subList: res.data.data,
        })
      },
      fail: function (err) { },
      complete: function () { }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  getCategory: function (id) {
    if (id == undefined) {
      id = 0
    }
    var that=this;
    wx.request({
      url: getApp().globalData.domain + 'api/category',
      data: {},
      header: {
        "Content-Type": "applciation/json"
      },
      method: "GET",
      success: function (res) {
        // console.log(res.data.data);
        that.setData({
          categoryList: res.data.data,
        })
        that.setHighlight(0)

      },
      fail: function (err) { },
      complete: function () { }
    })
  },
  setHighlight: function (index) {
    var that = this
    var highlight = []
    for (var i = 0; i < that.data.categoryList; i++) {
      highlight[i] = ''
    }
    highlight[index] = 'highlight'
    that.setData({ highlight: highlight })
  },
  getSubList: function (e) {
    if(e){
      var pid = e.currentTarget.dataset.id
      var index = e.currentTarget.dataset.index
    }else{
      var pid = 1
      var index = 0
    }
    this.setHighlight(index)
    var that = this;
    wx.request({
      url: getApp().globalData.domain + 'api/commodity/list?category_id=' + pid,
      data: { pid: pid },
      header: {
        "Content-Type": "applciation/json"
      },
      method: "GET",
      success: function (res) {
        // console.log(res.data.data);
        that.setData({
          subList: res.data.data,
        })
      },
      fail: function (err) { },
      complete: function () { }
    })
  },
  search: function () {
    wx.navigateTo({
      url: "../search/search"
    });
  }
})